/**
 * Arduino两轮小车
 * 
 * 本程序可不受限制的用于学习和教学，商业用途请联系作者。
 * 本程序不对用户因使用程序而造成任何形式的损失负责。
 * 
 * site: http://www.xpstem.com/maker/arduino-vehicle
 * author: billy zhang
 */
 
#ifndef ARDUINO_PS2X_RECEIVER_H_
#define ARDUINO_PS2X_RECEIVER_H_

#include <PS2X_lib.h>  //for v1.6

#include "RemoteReceiver.h"

/**
 * PS2控制器类
 * 
 * 接收PS2遥控器信号，转换为小车控制动作。
 * 需PS2接收器、PS2遥控器配套使用。
 */
class PS2xReceiver : public RemoteReceiver
{
   public:
     PS2xReceiver(uint8_t clkPin, uint8_t cmdPin, uint8_t datPin, uint8_t csPin)
      : clkPin_(clkPin), cmdPin_(cmdPin), datPin_(datPin), csPin_(csPin)
     {
     }

     void setup() override;
     void receive() override;

     uint8_t leftPressState() override { return leftPressState_; }
     uint8_t rightPressState() override { return rightPressState_; }
     uint8_t forwardPressState() override { return forwardPressState_; }
     uint8_t backwardPressState() override { return backwardPressState_; }
     uint8_t accelPressState() override { return accelPressState_; }
     uint8_t stopPressState() override { return stopPressState_; }
     
     int accelValue() override { return accelValue_; }
     int angleValue() override { return angleValue_; }
     
     ~PS2xReceiver();

  private:
     const uint8_t clkPin_; 
     const uint8_t cmdPin_; 
     const uint8_t datPin_; 
     const uint8_t csPin_;

     PS2X ps2x_; // create PS2 Controller Class
     int ps2x_error_ = 0;
     byte ps2x_type_ = 0;
     byte ps2x_vibrate_ = 0;

     uint8_t leftPressState_=0;
     uint8_t rightPressState_=0;
     uint8_t forwardPressState_=0;
     uint8_t backwardPressState_=0;
     uint8_t accelPressState_=0;
     uint8_t stopPressState_=0;
     
     int accelValue_=0;
     int angleValue_=0;
};

#endif
